﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OTC.POC.Repository.Repositories;
using OTC.POC.Repository.DataModel;
using OTC.POC.Repository;
using OTC.POC.Common;
using System.Data.Entity.Validation;
using OTC.POC.Common.UnitOfWork;

namespace OTC.POC.Business
{
    public class BaseDataService : IBaseDataService
    {
        public CommonRepository CommonRep { get; set; }
        public string[] DataBaseInfo;
        public IUnitOfWork UOW { get; set; }
        public ICacheService CacheSvr { get; set; }

        /// <summary>
        /// Get all system type details from cache
        /// </summary>
        /// <returns></returns>
        public List<SysTypeDetail> GetAllSysTypeDetail()
        {
            return CacheSvr.GetOrSet<List<SysTypeDetail>>("Cache_SysTypeDetail", () =>
            {
                HashSet<string> processed = new HashSet<string>();
                List<SysTypeDetail> res = CommonRep.GetDbSet<SysTypeDetail>().OrderBy(td => td.Seq).ToList();
                //res.ForEach(td =>
                //{
                //    if (!processed.Contains(td.TypeCode))
                //    {
                //        processed.Add(td.TypeCode);
                //        res.Add(new SysTypeDetail() { DetailName = "-- All --", DetailValue = "", TypeCode = td.TypeCode, Seq = 0 });
                //    }
                //});
                return res;
            });
        }

        public List<SysTypeDetail> GetSysTypeDetail(string strTypecode)
        {
            //return CommonRep.GetQueryable<SysTypeDetail>();

            var Result = GetAllSysTypeDetail().FindAll(d=>d.TypeCode == strTypecode);

            return Result;
        }

        //private string[] DataBaseInfoGet(string Id)
        //{
        //    DataBaseInfo = new string[3];
        //    string DetailId;
        //    SysType syst = GetSysType().Where(o => o.Id == Id)
        //               .Select(o => o).FirstOrDefault();
        //    if (syst != null)
        //    {
        //        DataBaseInfo[0] = syst.Id;
        //        DetailId = DataBaseInfo[0];
        //        DataBaseInfo[1] = GetSysTypeDetail().Where(o => o.Id == DetailId)
        //                    .Select(o => o.Id).FirstOrDefault();
        //    }

        //    return DataBaseInfo;

        //}
        
        public List<SysConfig> GetAllSysConfigs()
        {
            return CacheSvr.GetOrSet<List<SysConfig>>("Cache_SysConfig", () =>
            {
                return CommonRep.GetDbSet<SysConfig>().ToList();
            });
        }

        public SysConfig GetSysConfigByCode(string code)
        {
            return GetAllSysConfigs().Find(cfg => cfg.CfgCode == code);
        }
    }
}